package com.vf.admin.miaodong.service.impl;

import com.vf.admin.client.dto.AppNewDto;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterMessageRecordDao;
import com.vf.admin.miaodong.dao.AfterNewDao;
import com.vf.admin.miaodong.dao.AfterUserDao;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppNew;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AppNewService;
import com.vf.admin.utils.WYIM.WYMsg;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
/**
 * 会话
 */
@Service
public class AppNewServiceImpl implements AppNewService {

    @Autowired
    AfterNewDao afterNewDao;

    @Autowired
    AfterUserDao afterUserDao;

    @Autowired
    AfterMessageRecordDao afterMessageRecordDao;

    @Transactional
    @Override
    public Integer addConversation(Integer userid,Integer dfuseridid) {
        AppUser appUser = afterUserDao.get(userid);
        if (appUser == null) {
            return 1;
        }
        AppUser dfAppUser = afterUserDao.get(dfuseridid);
        if (dfAppUser == null) {
            return 1;
        }
        AppNew huiHa = afterNewDao.getHuiHa(userid, dfuseridid);
        if (huiHa == null) {
            AppNew appNew = new AppNew();
            appNew.setUserid(userid);
            appNew.setPic(appUser.getPic());
            appNew.setNick(appUser.getNick());
            appNew.setPhone(appUser.getLoginname());
            appNew.setDfuseridid(dfuseridid);
            appNew.setDfpic(dfAppUser.getPic());
            appNew.setDfnick(dfAppUser.getNick());
            appNew.setDfphone(dfAppUser.getLoginname());
            appNew.setCreatetime(new Date());
            afterNewDao.save(appNew);
        }
        AppNew nnn = afterNewDao.getHuiHa(dfuseridid, userid);
        if (nnn == null) {
            AppNew appNew = new AppNew();
            appNew.setUserid(dfuseridid);
            appNew.setPic(dfAppUser.getPic());
            appNew.setNick(dfAppUser.getNick());
            appNew.setPhone(dfAppUser.getLoginname());
            appNew.setDfuseridid(userid);
            appNew.setDfpic(appUser.getPic());
            appNew.setDfnick(appUser.getNick());
            appNew.setDfphone(appUser.getLoginname());
            appNew.setCreatetime(new Date());
            afterNewDao.save(appNew);
        }
        return 0;
    }

    @Override
    public void delConversation(Integer id) {
        afterNewDao.del(Integer.valueOf(id));
    }

    //获取会话列表
    @Transactional
    @Override
    public  Map<String, Object> getConversation(Integer userid,Integer pageno) throws Exception {
        SplitPageBean splitPageBean = new SplitPageBean(pageno,20);
        List<AppNewDto> conversation = afterNewDao.getConversation(Integer.valueOf(userid), splitPageBean);
        List<Map<String, Object>> ll = new ArrayList<>();
        for (AppNewDto appNew : conversation) {
            Map<String, Object> mm = new HashMap<>();
            //查询两人是否是好友
            //网易云查询会话历史消息
//            try{
            String res = WYMsg.querySessionMsg(appNew.getCode(), appNew.getDfcode());
            System.err.println(res);
            JSONObject jsonObject = new JSONObject(res);
            JSONArray msgs = jsonObject.getJSONArray("msgs");
            if (msgs.length() > 0) {
                JSONObject job = msgs.getJSONObject(0);
                JSONObject body = job.getJSONObject("body");
                Integer type1 = job.getInt("type");
                String msg = "";
                if (type1 == 1) {
                    msg = "[图片]";
                }
                if (type1 == 0) {
                    msg = (String) body.get("msg");
                }
                if (type1 == 2) {
                    msg = "[语音]";
                }
                if (type1 == 4) {
                    msg = "[位置]";
                }
                Object sendtime = job.get("sendtime");
                mm.put("sendtime", sendtime);
                mm.put("msg", msg);
            }
            mm.put("name", appNew.getDfnick());//接收这名称
            mm.put("initiator", appNew.getCode());//发送者
            mm.put("recipient", appNew.getDfcode());//接收者
            mm.put("recipientPic", appNew.getDfpic());//接收者头像
            mm.put("id", appNew.getId());
            mm.put("recipientid", appNew.getDfuseridid());
            ll.add(mm);
        }
        Map<String, Object> mmm = new HashMap<>();
        AppMessageRecord one = afterMessageRecordDao.getOne();
        if (one!=null) {
            mmm.put("xitong", one);
        }
        mmm.put("list", ll);
        mmm.put("total", splitPageBean.getTotalCount());
        return mmm;
    }

    @Override
    public Map<String, Object> getSystemMessage() {
        List<AppMessageRecord> appMessageRecords = afterMessageRecordDao.list();
        Map<String, Object> mm = new HashMap<>();
        mm.put("list", appMessageRecords);
        return mm;
    }

}
